const connection = require("../app/database");
class UserService {
  async create(user) {
    //1.获取用户

    const { username, password } = user;

    //2.拼接statement
    const statement = `INSERT INTO user (username,password) VALUES (?,?)`;
    //3.执行sql
    return await connection.execute(statement, [username, password]);
  }
  async findUserByName(name) {
    const statement = `SELECT * FROM user WHERE username = ?;`;
    const [values] = await connection.execute(statement, [name]);
    return values;
  }
  async queryAvatarWithUserId(userId) {
    const statement = `SELECT * FROM avatar WHERE user_id = ?;`;
    const [result] = await connection.execute(statement, [userId]);
    return result.pop();
  }
  async updateUserAvatar(avatarUrl, userId) {
    const statement = `UPDATE user SET avatar_url = ? WHERE id = ?;`;
    const [result] = await connection.execute(statement, [avatarUrl, userId]);
    return result;
  }
}
module.exports = new UserService();
